package com.example.springjwt.controllers;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.springjwt.auth.JwtUtil;
import com.example.springjwt.model.User;
import com.example.springjwt.model.request.LoginReq;
import com.example.springjwt.model.response.LoginRes;

@RestController
public class AuthController {



    private JwtUtil jwtUtil;
    public AuthController( JwtUtil jwtUtil) {

        this.jwtUtil = jwtUtil;

    }

    @ResponseBody
    @GetMapping(value = "/login")
    public ResponseEntity login(LoginReq loginReq)  {

           
            String email = loginReq.getEmail();
            User user = new User(email,"");
    		user.setFirstName("yuqi");
    		user.setLastName("bai");
    		user.getPermissions().add("product:write");
    		user.getPermissions().add("order:write");
            String token = null;
			try {
				token = jwtUtil.createToken(user);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            LoginRes loginRes = new LoginRes(email,token);

            return ResponseEntity.ok(loginRes);

      
    }
}